using AngleSharp.Dom;
using Viyi.Strings.Extensions;
using Viyi.Util.Linq;

namespace JamesFan.FetchAreaCodes.Parser;

class TopContentParser : ContentParser {
    public TopContentParser(CodeEntry baseEntry, string content) : base(baseEntry, content) { }

    protected override void ProcessDocument(IDocument document) {
        var parentCode = ParentCode;
        FindTheTable(document)?.QuerySelectorAll("a")
            .Select(el => (name: el.TextContent.Trim(), href: el.GetAttribute("href")))
            .WhereNot(it => it.name.IsEmpty() || it.href.IsEmpty())
            .ForEach(it => AddEntry(
                code: Path.GetFileNameWithoutExtension(it.href),
                name: it.name,
                link: it.href
            ));
    }
}